<script>
    var catId=<?php echo $model->getIsNewRecord() ? '0' : $model->catId; ?>;
    var relCatsData = '<?php echo $partnerRelCats ? implode(',', CHtml::listData($partnerRelCats, 'relCatId', 'relCatId')) : ''; ?>'.split(',');
    var relCats = new Array();
    <?php if(!empty($selectedCatSubCats)) : ?>
        var subCatsStack = '<?php echo implode(',', CHtml::listData($selectedCatSubCats, 'stackInAdmin', 'catId')); ?>'.split(','); 
    <?php else : ?>
       var subCatsStack = new Array();
    <?php endif; ?>    
    
    <?php if(!$model->getIsNewRecord()) : ?>    
        $(document).ready(function() {
            if(relCatsData) {
                for(var i=0; i<relCatsData.length; i++)
                    relCats[relCatsData[i]] = relCatsData[i];
            }
            oldCatId = subCatsStack.length ? subCatsStack[0] : $(".cats :selected").val();
            checkType();
        });
    <?php endif; ?>    
</script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/admin/admin_partner.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
    tinyMCE.init({
        mode : "exact",
        elements : "partnerAbout",
        theme : "advanced",
        skin : "default",
        width : "850",
        height : "210",
       
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,search,replace,|,bullist,numlist,|,undo,redo,|,sub,sup,|,formatselect,fontselect,fontsizeselect,|,code",
        
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : false
    });
</script>


<div class="content">
<ul class="breadcrumb">
  <li><?php echo CHtml::link('Админка ', array('default/index')); ?> <span class="divider">/</span></li>
       <li> <?php echo CHtml::link(' Партнерки ', array('default/partner'));?> <span class="divider">/</span></li>
        <?php if(isset($_GET['id'])) : ?>
            <li class="active">Изменить</li>
        <?php else : ?>
            <li class="active">Добавить</li>
        <?php endif; ?>    
    </ul>
     <?php if(isset($_GET['id'])) : ?>
            <h2>Редактирование партнерки</h2>
            <input type="hidden" value="<?php  echo($_GET['id']);  ?>" id="partner_id" />
        <?php else : ?>
            <h2>Создание партнерки</h2>
        <?php endif; ?>
    <hr/>
    <?php echo CHtml::errorSummary($model); ?>
    <?php echo CHtml::form('', 'post', array('id'=>'partnerForm')); ?>
    <?php echo CHtml::activeHiddenField($model, 'relCat', array('value'=>'', 'id'=>'relCat')); ?>
     
    <div class="controls controls-row">
         <div class="span8">   
            <?php echo CHtml::activeLabel($model, 'name', array('class'=>'text-info')); ?>
            <?php echo CHtml::activeTextField($model, 'name', array('class'=>'activeTextField', 'placeholder'=>$model->getAttributeLabel('name'))); ?>
         </div>
         <div class="span3">
             <label class="text-info " for="Partner_isClose">Закрыть партнерку</label>
             <?php echo CHtml::activeCheckBox($model, 'isClose'); ?> да
             <label class="text-info " for="Partner_zeroPosition">Закрепить на 0 позиции</label>
             <?php echo CHtml::activeCheckBox($model, 'zeroPosition'); ?> да
         </div>    
    </div>
    <?php echo CHtml::activeLabel($model, 'about', array('class'=>'text-info')); ?>
    <?php echo CHtml::activeTextArea($model, 'about', array('class'=>'activeTextArea', 'placeholder'=>$model->getAttributeLabel('about'), 'id'=>'partnerAbout')); ?>
    <br/>
    <div class="controls controls-row">
        <div class="span3">    
            <?php echo CHtml::activeLabel($model, 'site', array('class'=>'text-info')); ?>
            <?php echo CHtml::activeTextField($model, 'site', array('class'=>'activeTextField', 'placeholder'=>$model->getAttributeLabel('site'))); ?> <br/>
        </div>
        <div class="span5">    
            <label class="text-info" for="Partner_openDate">Дата открытия, формат: день.месяц.гггг(10.11.2012)</label>
            <?php echo CHtml::activeTextField($model, 'openDate', array('class'=>'activeTextField', 'placeholder'=>$model->getAttributeLabel('openDate'))); ?>
        </div>
    </div>    
    
    <fieldset>
        <legend>SEO параметры</legend>    
        <div class="controls controls-row">
            <div class="span3">
                <?php echo CHtml::activeLabel($model, 'url', array('class'=>'text-info ')); ?>
                <?php echo CHtml::activeTextField($model, 'url', array('class'=>'activeTextField', 'placeholder'=>$model->getAttributeLabel('url'))); ?>
            </div>
            <div class="span5"> 
                <?php echo CHtml::activeLabel($model, 'title', array('class'=>'text-info ')); ?>
                <?php echo CHtml::activeTextField($model, 'title', array('class'=>'activeTextField', 'placeholder'=>$model->getAttributeLabel('title'))); ?> 
            </div>   
            <div class="span3">
                <?php echo CHtml::activeLabel($model, 'keywords', array('class'=>'text-info')); ?>
                <?php echo CHtml::activeTextField($model, 'keywords', array('class'=>'activeTextArea', 'placeholder'=>$model->getAttributeLabel('keywords'))); ?>
            </div>    
        </div> 
    </fieldset>
     <fieldset>
    <legend>Тип партнерской программы</legend>    
       <div class="controls controls-row">
           <?php echo CHtml::activeDropDownList($model, 'type', array('Система инвайтов'=>'Система инвайтов', 'Простая регистрация'=>'Простая регистрация'), array('class'=>'type')); ?>
           <?php if($model->getIsNewRecord()): ?>
               <div id="invitelist" class="hide">
                   Сприсок инвайтов: <img src="/images/admin/plus_orange.png" class="addInvite" style="cursor: pointer"/><hr/>
                   <div id="inviteWrap"></div>
               </div> 
               <div id="reflink">
                    <?php echo CHtml::activeLabel($model, 'refLink', array('class'=>'text-info')); ?>
                    <?php echo CHtml::activeTextField($model, 'refLink', array('class'=>'activeTextField', 'placeholder'=>$model->getAttributeLabel('refLink'))); ?>
               </div>        
            <?php else : ?>
                <div class="controls">
                    <div id="invitelist" <?php if($model->type == 'Простая регистрация'){?>class="hide"<?php } ?>>
                        Сприсок инвайтов: <img src="/images/admin/plus_orange.png" class="addInvite" style="cursor: pointer"/><hr/>
                        <div id="inviteWrap">
                            <?php foreach($partnerData->invite as $key => $invite) {  ?>    
                                <div id="inviteContainer<?php echo($key) ?>" class="form-inline" style="margin-bottom:10px;"><label class="text-info " for="invites_[<?php echo($key) ?>]">Инвайт:</label>
                                <input class="inviteInput input-xlarge" id="invites_[<?php echo($key) ?>]" type="text" name="Partner[invites][val][<?php echo($key) ?>]" value="<?php echo($invite['val']) ?>"> 
                                &nbsp;<label class="checkbox"><input class="invateCheckBox<?php echo($key) ?>" <?php if($partnerData->invite[$key]['notWork']==true) {echo('checked=""');} ?> type="checkbox" name="Partner[invites][notWork][<?php echo($key) ?>]">  Не работает </label>&nbsp;
                                <img src="/images/admin/deletered.png" class="delButton" style="cursor:pointer"/></div>
                            <?php } ?>
                        </div>
                    </div> 

                    <div id="reflink" <?php if($model->type == 'Система инвайтов'){?>class="hide"<?php } ?>>
                        <?php echo CHtml::activeLabel($model, 'refLink', array('class'=>'text-info')); ?>
                        <?php echo CHtml::activeTextField($model, 'refLink', array('class'=>'activeTextField', 'placeholder'=>$model->getAttributeLabel('refLink'))); ?>
                    </div>            
                 </div>   
            <?php endif; ?>    
        </div> 
    </fieldset>
    <fieldset>
        <legend>Оценка редактора</legend> 
         <?php echo CHtml::activeRadioButtonList($model, 'editorMark', array('1'=>'1', '2'=>'2', '3'=>'3'), array('separator'=>' ', 'labelOptions'=>array('style'=>'display:inline'))); ?>
        <div style="height: 10px;"></div>
    </fieldset>
    <fieldset>
    <legend>Параметры партнерской программы</legend>   
    <div class="controls controls-row"> 
        <div class="span4"><?php echo CHtml::activeLabel($model, 'catId', array('class'=>'text-info ')); ?> 
            <?php echo CHtml::activeDropDownList($model, 'catId', CMap::mergeArray(CHtml::listData($cats, 'catId', 'name'), array('0'=>'')), array('class'=>'cats')); ?> 
        </div> 
        <div class="span4">
            <div id="subcatsList" >
                <?php 
                if(!empty($partnerSubCats)) { 
                    $i = 0;
                    foreach ($partnerSubCats as $c) { 
                        if(! ++$i % 10) echo '<br/>';
                        echo $c['name'];
                        $selected = false;
                        if(!empty($selectedCatSubCats)) {
                            for($l=0; $l<count($selectedCatSubCats); $l++) {
                                if($selectedCatSubCats[$l]['catId'] == $c['catId']) $selected = true;
                            }
                        }
                        echo CHtml::checkBox('Partner[subCats][]', $selected, array('value'=>$c['catId'], 'style'=>'margin-right:15px', 'class'=>'listSubCats'));
                    }
                }; ?>
            </div>
            <?php echo CHtml::activeHiddenField($model, 'stackData', array('value'=>'', 'class'=>'stackData')); ?>
        </div>
    </div> 
    <hr/> 
    <div class="controls controls-row">
        <div class="span4">   
            <label class="text-info " for="relCats">Партнерка частично подходит и под другие категории: </label>
                <?php echo CHtml::dropDownList('', '0', CMap::mergeArray(CHtml::listData($allCats, 'catId', 'name'), array('0'=>'Выбери категорию')), array('id'=>'relCats')); ?> <br/>
                <div class="relParamList">
                    <?php if(!empty($partnerRelCats)) : ?>  
                       <?php for($i=0; $i<count($partnerRelCats); $i++) : ?>
                            <div>
                                <label class="text-info " for="relcats_[<?php echo $i; ?>]">Категория: <b><?php echo($partnerRelCats[$i]['name']);  ?></b></label>
                                <span>Подсказка</span> 
                                <img src="/images/admin/deletered.png" class="delInput" style="cursor:pointer" id="catId<?php echo $partnerRelCats[$i]['catId']; ?>"/>
                                <input class="relcats_input input-xlarge" value="<?php echo($partnerRelCats[$i]['val']);  ?>" type="text" name="Partner[aboutRelCat][<?php echo $i; ?>]" id="relcats_[<?php echo $i; ?>]" >
                                <input  type="hidden" name="Partner[relCat][]" value="<?php echo($partnerRelCats[$i]['relCatId']);  ?>" /> 
                            </div><br/>
                       <?php endfor; ?> 
                    <?php endif; ?>
                </div>
        </div>
       <div class="span4">
            Урлы связанных профилей: <img src="/images/admin/plus_orange.png" class="addRelProfiles" style="cursor: pointer"/><br/><br/>
            <div class="listRelProfiles">
                <?php
                if(!empty($partnerData->relPartners)) {
                    foreach ($partnerData->relPartners as $key => $value) {   ?>   
                        <div>
                            <br/>Удалить: <img src="/images/admin/deletered.png" class="delButton" style="cursor:pointer"/><br/>
                            <label class="text-info " for="">Урл связанного профиля:&nbsp;</label><input class="relProfilesVal input-xlarge" type="text" name="Partner[relProfiles][]" value="<?php echo($value['url']); ?>" >
                            <label class="text-info " for="">Анкор:&nbsp;</label><input class=" input-xlarge" type="text" name="Partner[relProfilesTitle][]" value="<?php echo($value['title']); ?>" >
                        </div>
                <?php }
                } ?>  
            </div>
       </div> 
        
    </div>  
         <hr/>
    <div class="controls controls-row">
        <div class="listSortParams" style="width: 100%">
            <?php if(!empty($sortParams)) : ?>
                <div id="sortParamsWrap">
                    <?php 
                    $sort1 = $sort2 = array();
                    for($i = 0; $i<count($sortParams); $i++) {
                        if($sortParams[$i]['type'] == 1) $sort1[] = $sortParams[$i];
                        else $sort2[] = $sortParams[$i];
                    }?>
                    
                    <div class="span4">   
                        <strong>Параметры 1 сорта:</strong><hr/>
                        <div id="sortParams1" >
                            <div id="sortList1">
                                <div class="controls controls-row"> 
                                    <?php foreach($sort1 as $s) : ?>
                                        <label class="text-info " for="Partner_sort1_catId<?php echo($s['catRatingId']); ?>"><?php echo($s['crval']); ?>:</label>
                                        <input class="input-xlarge" placeholder="Значение 1 параметра" type="text" value="<?php echo $s['val']; ?>" name="Partner[sort1][catId<?php echo($s['catRatingId']); ?>]" id="Partner_sort1_catId<?php echo($s['catRatingId']); ?>">
                                    <?php endforeach; ?>   
                                </div>
                            </div>
                       </div>
                    </div>

                    <div class="span4">  
                        <strong>Параметры 2 сорта:</strong><hr/>
                        <div id="sortParams2">
                            <div id="sortList2">
                                <div id="sortList2">
                                    <div class="controls controls-row"> 
                                        <?php foreach($sort2 as $s) { ?>
                                            <label class="text-info " for="Partner_sort2_catId<?php echo($s['catRatingId']); ?>"><?php echo($s['crval']); ?>:</label>
                                            <input class="input-xlarge" placeholder="Значение параметра" type="text" value="<?php echo $s['val']; ?>" name="Partner[sort2][catId<?php echo($s['catRatingId']); ?>]" id="Partner_sort2_catId<?php echo($s['catRatingId']); ?>">
                                        <?php  } ?>   
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        </div>
        <br/>
    </div>
    </fieldset>
    <fieldset>
        <legend>Представители партнерки</legend>    
        <?php  if(!$model->getIsNewRecord()) : ?>
            <?php $model->delegate1 = $partnerData->delegate1; ?>
            <?php $model->delegate2 = $partnerData->delegate2; ?>
        <?php endif; ?>
        <div class="controls controls-row"> 
            <div class="span2" style="margin-right: 50px;">
                <?php echo CHtml::activeLabel($model, 'delegate1',  array('class'=>'text-info ')); ?>
                <?php echo CHtml::activeTextField($model, 'delegate1', array('class'=>'delegate', 'placeholder'=>$model->getAttributeLabel('delegate1'), 'autocomplete' => 'off')); ?>
                <div id="Partner_delegate1Complit"></div>     
                <span class="help-block">Ник пользователя</span>    
            </div>    
            <div class="span2" style="margin-right: 50px;">
                <?php echo CHtml::activeLabel($model, 'delegate2', array('class'=>'text-info ')); ?>
                <?php echo CHtml::activeTextField($model, 'delegate2', array('class'=>'delegate', 'placeholder'=>$model->getAttributeLabel('delegate2'), 'autocomplete' => 'off')); ?>
                <div id="Partner_delegate2Complit"></div>      
                <span class="help-block small">Ник пользователя</span>
            </div> 
            <div class="span3" style="margin-top: 20px;"> 
                <?php echo CHtml::activeLabel($model, 'fieldName',  array('class'=>'text-info ')); ?>
                <?php echo CHtml::activeTextField($model, 'fieldName', array('class'=>'activeTextArea', 'placeholder'=>$model->getAttributeLabel('fieldName'))); ?>      
            </div>    
        </div>
    </fieldset>
    
    <fieldset>
        <legend>Дополнительные(текстовые) поля оценивания отзыва:</legend>  
        <div class="controls controls-row">
            Добавить поле: <img src="/images/admin/plus_orange.png" class="addRatingTextField" style="cursor: pointer"/><br/><br/>
            
            <div class="ratingTextFieldsWrap">
                <?php if(!empty($ratingTextFields)) : ?>
                    <?php foreach ($ratingTextFields as $item) : ?> 
                        <br/><div>
                            Удалить:<img src="/images/admin/deletered.png" class="delButton" style="cursor:pointer"/><br/>
                            Заголовок:<br/>
                            <input type="text" name="Partner[ratingTextField][]" value="<?php echo $item['title']; ?>" /><br/>
                            Описание:<br/>
                            <textarea class="activeTextArea" name="Partner[aboutRatingTextField][]" placeholder="Описание" style="width:350px; height:20px"><?php echo $item['about']; ?></textarea>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>    
        </div> 
    </fieldset>
    <p></p>           
        
    
    <?php echo CHtml::button('Подтвердить', array('class'=>'btn btn-large btn-primary', 'id'=>'submitButton')); ?>
    <a class="btn btn-large" href="/admin/default/partner">Отмена</a>
    <?php echo CHtml::endForm(); ?> 
</div>
